﻿using HCDV;
using HCDV.HCSP;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;

namespace hrainwebbase.business
{
    /// <summary>
    /// 业务检查类
    /// </summary>
    public class Bs_Check
    {
        /// <summary>
        /// 唯一性检查
        /// </summary>
        /// <param name="ywcheckonlyValue">当前检查信息</param>
        /// <param name="allinfo">所有字段</param>
        /// <param name="addOrupdate">1:add 0:update</param>
        public static void ywcheckonly(string ywcheckonlyValue, Hashtable inInfo, int addOrupdate, HttpContext Mcontext)
        {
            if (!string.IsNullOrEmpty(ywcheckonlyValue))
            {
                Hashtable allinfo = (Hashtable)inInfo.Clone();
                var userInfo = (LoginUser)(Mcontext.Session["LoginUser"]);
                if (!allinfo.ContainsKey("fdataadduserid") && !allinfo.ContainsKey("fdatabranchid"))
                {
                    allinfo.Add("fdataadduserid", userInfo.UserId);
                    allinfo.Add("fdatabranchid", userInfo.UserOrg);
                    allinfo.Add("fdatadepid", userInfo.UserDep);
                    allinfo.Add("fdatadepname", userInfo.UserDepName);
                    allinfo.Add("fdatabranchname", userInfo.UserOrgName);
                    allinfo.Add("fdataaddusername", userInfo.UserName);
                }

                //<input type="hidden" id="ywcheckonly" name="ywcheckonly" value="ftype,fnv1#当前序号已存在!" /><!--唯一性检查-->

                string[] checkvalue = ywcheckonlyValue.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
                string[] fileidname = checkvalue[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                string showinfo = checkvalue[1];
                string tablename = allinfo["tablename"].ToString();
                string whereInfo = "";
                string sql = "select * from " + tablename + " where {0}";
                foreach (var f in fileidname)
                {
                    if (whereInfo.Length == 0)
                    {
                        whereInfo = " " + f.Substring(1) + " = '" + allinfo[f].ToString() + "' ";
                    }
                    else
                    {
                        whereInfo += " and " + f.Substring(1) + " = '" + allinfo[f].ToString() + "' ";
                    }
                }
                if (addOrupdate == 0)
                {

                    whereInfo += " and  id  <> " + allinfo["id"].ToString();
                }
                DataTable dt;
                using (dbsimple db = new dbsimple())
                {
                    dt = db.Query(string.Format(sql, whereInfo));
                }
                if (dt != null && dt.Rows.Count > 0)
                {
                    throw new Exception(showinfo);
                }
            }
        }
    }
}